package cn.otrue.patienthealthmanager.view;

import java.text.DecimalFormat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

public class StatisticsPopupWindow extends PopupWindow {
	private Context mContext;
	private View rootView;
	private LinearLayout ll_statistics_sugar;
	private LinearLayout ll_statistics_pressure;
	private LinearLayout ll_statistics_plus;
	private LinearLayout ll_statistics_other;
	private LinearLayout ll_statistics_otherinfo;
	private TextView tv_statistics_othernormal;
	private TextView tv_statistics_otherabnormal;
	private TextView tv_statistics_otherinfo;

	private TextView tv_sugar_normal_percentage;// 血糖中圆中的正常时的id：多天
	private TextView tv_sugar_Heighter_percentage;// 血糖中圆中的偏高时的id：多天
	private TextView tv_sugar_lower_percentage;// 血糖中圆中的偏低时的id：多天
	private TextView tv_sugar_normal_times;// 血糖中正常时的次数id：单天和多天共用//血压心率的单天多天也使用
	private TextView tv_sugar_Heighter_times;// 血糖偏高时的次数id：单天和多天共用//血压心率的单天多天也使用
	private TextView tv_sugar_lower_times;// 血糖偏低时的次数id：单天和多天共用//血压心率的单天多天也使用
	private TextView tv_sugar_beforeat_highest;// 血糖餐前最高值：单天和多天共用
	private TextView tv_sugar_beforeat_lowest;// 血糖餐前最低值：单天和多天共用
	private TextView tv_sugar_beforeat_average;// 血糖餐前平均值：单天和多天共用
	private TextView tv_sugar_beforeat_percentage;// 血糖餐前百分比
	private TextView tv_sugar_aftereat_highest;// 血糖餐后最高值：单天和多天共用
	private TextView tv_sugar_aftereat_lowest;// 血糖餐后最低值：单天和多天共用
	private TextView tv_sugar_aftereat_average;// 血糖餐后平均值：单天和多天共用
	private TextView tv_sugar_aftereat_percentage;// 血糖餐后百分比

	private TextView tv_pressure_normal;// 血压正常圆中正常时的id:多天
	private TextView tv_pressure_heighter;
	private TextView tv_pressure_lower;
	private TextView tv_pressure_percentage;
	private TextView tv_pressure_normal_times;// 血压共用
	private TextView tv_pressure_Heighter_times;
	private TextView tv_pressure_lower_times;
	private TextView tv_pressure_heightst;
	private TextView tv_pressure_lowst;
	private TextView tv_pressure_average;

	private TextView tv_heart_normal;// 心率圆中正常时id:多天
	private TextView tv_heart_heighter;
	private TextView tv_heart_lower;
	private TextView tv_heart_percentage;
	private TextView tv_heart_normal_times;// 心率共用
	private TextView tv_heart_heighter_times;
	private TextView tv_heart_lower_times;
	private TextView tv_heart_heightst;
	private TextView tv_heart_lowst;
	private TextView tv_heart_average;
	private ImageView iv_close_pop;
	private DecimalFormat df;

	/**
	 * 
	 * @param context
	 * @param type
	 * @param data
	 */
	public StatisticsPopupWindow(Context context, int type, String[] data,
			boolean isSugarDay, boolean isplus_heart_Day) {
		super(context);
		mContext = context;
		df = new DecimalFormat("#.00");
		LayoutInflater infalater = LayoutInflater.from(context);
		switch (type) {
		case MyConst.SUGAR:
			if (isSugarDay) {
				rootView = infalater.inflate(R.layout.layout_sugar_statistics_pop, null);
			} else {
				rootView = infalater.inflate(R.layout.layout_sugar_day_pop,null);
			}
			break;
		case MyConst.PLUS_PRESSURE:
			if (isplus_heart_Day) {
				rootView = infalater.inflate(R.layout.layout_pressure_plus_day_pop, null);
			} else {
				rootView = infalater.inflate(R.layout.layout_plus_heartrate_statistics_pop, null);
			}
			break;
		default:
			rootView = infalater.inflate(R.layout.layout_statistics_window,
					null);
			break;
		}
		initView(rootView, type, data, isSugarDay, isplus_heart_Day);

		// 设置SelectPicPopupWindow的View
		this.setContentView(rootView);
		this.setBackgroundDrawable(mContext.getResources().getDrawable(
				R.drawable.data_show_wb));
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(CommonUtil.dip2px(context, 330));
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		this.setOutsideTouchable(true);

	}

	public void initView(View rootView, int type, String[] data,
			boolean isSugarDay, boolean isplus_heart_Day) {
		
		switch (type) {
		case MyConst.SUGAR:
			iv_close_pop = (ImageView) rootView.findViewById(R.id.iv_close_pop);
			iv_close_pop.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					StatisticsPopupWindow.this.dismiss();
				}
			});
			tv_sugar_normal_times = (TextView) rootView
					.findViewById(R.id.tv_sugar_normal_times);
			tv_sugar_Heighter_times = (TextView) rootView
					.findViewById(R.id.tv_sugar_Heighter_times);
			tv_sugar_lower_times = (TextView) rootView
					.findViewById(R.id.tv_sugar_lower_times);
			tv_sugar_beforeat_highest = (TextView) rootView
					.findViewById(R.id.tv_sugar_beforeat_highest);
			tv_sugar_beforeat_lowest = (TextView) rootView
					.findViewById(R.id.tv_sugar_beforeat_lowest);
			tv_sugar_beforeat_average = (TextView) rootView
					.findViewById(R.id.tv_sugar_beforeat_average);
			tv_sugar_aftereat_highest = (TextView) rootView
					.findViewById(R.id.tv_sugar_aftereat_highest);
			tv_sugar_aftereat_lowest = (TextView) rootView
					.findViewById(R.id.tv_sugar_aftereat_lowest);
			tv_sugar_aftereat_average = (TextView) rootView
					.findViewById(R.id.tv_sugar_aftereat_average);
			if (isSugarDay) {

			} else {
				tv_sugar_normal_percentage = (TextView) rootView
						.findViewById(R.id.tv_sugar_normal_percentage);
				tv_sugar_Heighter_percentage = (TextView) rootView
						.findViewById(R.id.tv_sugar_Heighter_percentage);
				tv_sugar_lower_percentage = (TextView) rootView
						.findViewById(R.id.tv_sugar_lower_percentage);
				tv_sugar_beforeat_percentage = (TextView) rootView
						.findViewById(R.id.tv_sugar_beforeat_percentage);
				tv_sugar_aftereat_percentage = (TextView) rootView
						.findViewById(R.id.tv_sugar_aftereat_percentage);
			}
			break;
		case MyConst.PLUS_PRESSURE:
			iv_close_pop = (ImageView) rootView.findViewById(R.id.iv_close_pop);
			iv_close_pop.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					StatisticsPopupWindow.this.dismiss();
				}
			});
			tv_pressure_normal_times = (TextView) rootView
					.findViewById(R.id.tv_pressure_normal_times);// 血压共用
			tv_pressure_Heighter_times = (TextView) rootView
					.findViewById(R.id.tv_pressure_Heighter_times);
			tv_pressure_lower_times = (TextView) rootView
					.findViewById(R.id.tv_pressure_lower_times);
			tv_pressure_heightst = (TextView) rootView
					.findViewById(R.id.tv_pressure_heightst);
			tv_pressure_lowst = (TextView) rootView
					.findViewById(R.id.tv_pressure_lowst);
			tv_pressure_average = (TextView) rootView
					.findViewById(R.id.tv_pressure_average);
			tv_heart_normal_times = (TextView) rootView
					.findViewById(R.id.tv_heart_normal_times);// 心率共用
			tv_heart_heighter_times = (TextView) rootView
					.findViewById(R.id.tv_heart_heighter_times);
			tv_heart_lower_times = (TextView) rootView
					.findViewById(R.id.tv_heart_lower_times);
			tv_heart_heightst = (TextView) rootView
					.findViewById(R.id.tv_heart_heightst);
			tv_heart_lowst = (TextView) rootView
					.findViewById(R.id.tv_heart_lowst);
			tv_heart_average = (TextView) rootView
					.findViewById(R.id.tv_heart_average);
			if (isplus_heart_Day) {

			} else {
				tv_pressure_normal = (TextView) rootView
						.findViewById(R.id.tv_pressure_normal);
				tv_pressure_heighter = (TextView) rootView
						.findViewById(R.id.tv_pressure_heighter);
				tv_pressure_lower = (TextView) rootView
						.findViewById(R.id.tv_pressure_lower);
				tv_pressure_percentage = (TextView) rootView
						.findViewById(R.id.tv_pressure_percentage);
				tv_heart_normal = (TextView) rootView
						.findViewById(R.id.tv_heart_normal);// 心率圆中正常时id:多天
				tv_heart_heighter = (TextView) rootView
						.findViewById(R.id.tv_heart_heighter);
				tv_heart_lower = (TextView) rootView
						.findViewById(R.id.tv_heart_lower);
				tv_heart_percentage = (TextView) rootView
						.findViewById(R.id.tv_heart_percentage);
			}
			break;
		default:
			ll_statistics_sugar = (LinearLayout) rootView
					.findViewById(R.id.ll_statistics_sugar);
			ll_statistics_pressure = (LinearLayout) rootView
					.findViewById(R.id.ll_statistics_pressure);
			ll_statistics_plus = (LinearLayout) rootView
					.findViewById(R.id.ll_statistics_plus);
			ll_statistics_other = (LinearLayout) rootView
					.findViewById(R.id.ll_statistics_other);
			ll_statistics_otherinfo = (LinearLayout) rootView
					.findViewById(R.id.ll_statistics_otherinfo);
			break;
		}

		tv_statistics_othernormal = (TextView) rootView
				.findViewById(R.id.tv_statistics_othernormal);
		tv_statistics_otherabnormal = (TextView) rootView
				.findViewById(R.id.tv_statistics_otherabnormal);

		tv_statistics_otherinfo = (TextView) rootView
				.findViewById(R.id.tv_statistics_otherinfo);

		switch (type) {
		case MyConst.SUGAR:
			DecimalFormat format = new DecimalFormat("#.0");
			tv_sugar_normal_times.setText(data[1]+"次");// 血糖正常次数
			tv_sugar_Heighter_times.setText(data[9]+"次");// 血糖偏高次数
			tv_sugar_lower_times.setText(data[6]+"次");// 血糖偏低次数
			if (data[0] == null) {
				tv_sugar_beforeat_highest.setText(data[0]);// 餐前最高血糖
			} else {
				double beforSugarH = Double.parseDouble(data[0]);
				if (beforSugarH > 0) {
					tv_sugar_beforeat_highest.setText(format
							.format(beforSugarH));
				} else {
					tv_sugar_beforeat_highest.setText(data[0]);
				}
			}
			if (data[11] == null) {
				tv_sugar_beforeat_lowest.setText(data[11]);// 餐前最低血糖
			} else {
				double beforSugarL = Double.parseDouble(data[11]);
				if (beforSugarL > 0) {
					tv_sugar_beforeat_lowest
							.setText(format.format(beforSugarL));// 餐前最低血糖
				} else {
					tv_sugar_beforeat_lowest.setText(data[11]);// 餐前最低血糖
				}

			}

			if (data[4] == null) {
				tv_sugar_beforeat_average.setText(data[4]);
			} else {
				double beforAvage = Double.parseDouble(data[4]);
				if (beforAvage > 0) {
					tv_sugar_beforeat_average.setText(df.format(beforAvage));// 餐前平均
				} else {
					tv_sugar_beforeat_average.setText(data[4]);
				}
			}
			if (data[8] == null) {
				tv_sugar_aftereat_highest.setText(data[8]);// 餐后血糖最高
			} else {
				double afterSugarH = Double.parseDouble(data[8]);
				if (afterSugarH > 0) {
					tv_sugar_aftereat_highest.setText(format
							.format(afterSugarH));// 餐后血糖最高
				} else {
					tv_sugar_aftereat_highest.setText(data[8]);// 餐后血糖最高
				}
			}
			if (data[5] == null) {
				tv_sugar_aftereat_lowest.setText(data[5]);// 餐后血糖最低
			} else {
				double afterSugarL = Double.parseDouble(data[5]);
				if (afterSugarL > 0) {
					tv_sugar_aftereat_lowest.setText(format.format(afterSugarL));// 餐后血糖最低
				} else {
					tv_sugar_aftereat_lowest.setText(data[5]);// 餐后血糖最低
				}
			}

			if (data[13] == null) {
				tv_sugar_aftereat_average.setText(data[13]);// 餐后平均值
			} else {
				double afterAverage = Double.parseDouble(data[13]);
				if (afterAverage > 0) {
					tv_sugar_aftereat_average.setText(df.format(afterAverage));// 餐后平均值
				} else {
					tv_sugar_aftereat_average.setText(data[13]);// 餐后平均值
				}
			}
			if (isSugarDay) {

			} else {
				if (data[12] == null) {
					tv_sugar_normal_percentage.setText("正常0%");// 血糖正常百分比
				} else {
					tv_sugar_normal_percentage.setText("正常" + data[12] + "%");// 血糖正常百分比
				}
				if (data[3] == null) {
					tv_sugar_Heighter_percentage.setText("偏高0%");// 血糖偏高百分比
				} else {
					tv_sugar_Heighter_percentage.setText("偏高" + data[3] + "%");// 血糖偏高百分比
				}
				if (data[2] == null) {
					tv_sugar_lower_percentage.setText("偏低0%");// 血糖偏低百分比
				} else {
					tv_sugar_lower_percentage.setText("偏低" + data[2] + "%");// 血糖偏低百分比
				}
				if (data[10] == null) {
					tv_sugar_beforeat_percentage.setText("0%");
				} else {
					tv_sugar_beforeat_percentage.setText(data[10] + "%");// 血糖餐前百分比：异常百分比
				}
				if (data[7] == null) {
					tv_sugar_aftereat_percentage.setText("0%");
				} else {
					tv_sugar_aftereat_percentage.setText(data[7] + "%");// 血糖餐后百分比：异常百分比
				}

			}
			break;
		case MyConst.PLUS_PRESSURE:
			tv_pressure_normal_times.setText(data[14]+"次");
			tv_pressure_Heighter_times.setText(data[16]+"次");
			tv_pressure_lower_times.setText(data[10]+"次");
			if (data[7] == null) {
				tv_pressure_heightst.setText("0");
			} else {
				tv_pressure_heightst.setText("" + Double.parseDouble(data[7]));
			}
			if (data[0] == null) {
				tv_pressure_lowst.setText("0");
			} else {
				tv_pressure_lowst.setText("" + Double.parseDouble(data[0]));
			}
			if (data[1] == null) {
				tv_pressure_average.setText(data[1]);
			} else {
				double plusData = Double.parseDouble(data[1]);
				if (plusData > 0) {
					tv_pressure_average.setText(df.format(plusData));
				} else {
					tv_pressure_average.setText(data[1]);
				}
			}

			tv_heart_normal_times.setText(data[8]+"次");
			tv_heart_heighter_times.setText(data[6]+"次");
			tv_heart_lower_times.setText(data[13]+"次");
			if (data[12] == null) {
				tv_heart_heightst.setText("0");
			} else {
				tv_heart_heightst.setText("" + Double.parseDouble(data[12]));
			}
			if (data[3] == null) {
				tv_heart_lowst.setText("0");
			} else {
				tv_heart_lowst.setText("" + Double.parseDouble(data[3]));
			}
			if (data[9] == null) {
				tv_heart_average.setText(data[9]);
			} else {
				double hertData = Double.parseDouble(data[9]);
				if (hertData > 0) {
					tv_heart_average.setText(df.format(hertData));
				} else {
					tv_heart_average.setText(data[9]);
				}
			}

			if (isplus_heart_Day) {

			} else {
				if (data[17] == null) {
					tv_pressure_normal.setText("正常0%");
				} else {
					tv_pressure_normal.setText("正常" + data[17] + "%");
				}
				if (data[15] == null) {
					tv_pressure_heighter.setText("偏高0%");
				} else {
					tv_pressure_heighter.setText("偏高" + data[15] + "%");
				}
				if (data[11] == null) {
					tv_pressure_lower.setText("偏低0%");
				} else {
					tv_pressure_lower.setText("偏低" + data[11] + "%");
				}
				if (data[19] == null) {
					tv_pressure_percentage.setText("0%");
				} else {
					tv_pressure_percentage.setText(data[19] + "%");
				}

				tv_heart_normal.setText("正常"+data[18]+"%");
				tv_heart_heighter.setText("偏高"+data[4]+"%");
				tv_heart_lower.setText("偏低"+data[5]+"%");
				if (data[2] == null) {
					tv_heart_percentage.setText("0%");
				} else {
					tv_heart_percentage.setText(data[2] + "%");
				}

			}
			break;
		case MyConst.OTHER:
			ll_statistics_sugar.setVisibility(View.GONE);
			ll_statistics_pressure.setVisibility(View.GONE);
			ll_statistics_plus.setVisibility(View.GONE);
			ll_statistics_other.setVisibility(View.VISIBLE);
			ll_statistics_otherinfo.setVisibility(View.GONE);
			tv_statistics_othernormal.setText(data[0] + "天");
			tv_statistics_otherabnormal.setText(data[1] + "天");

			break;
		case MyConst.OTHER_INFO:
			ll_statistics_sugar.setVisibility(View.GONE);
			ll_statistics_pressure.setVisibility(View.GONE);
			ll_statistics_plus.setVisibility(View.GONE);
			ll_statistics_other.setVisibility(View.GONE);
			ll_statistics_otherinfo.setVisibility(View.VISIBLE);
			String other = "";
			for (int i = 0; i < data.length; i++) {
				other = other + data[i] + " ";
			}
			tv_statistics_otherinfo.setText(other);
			break;
		default:
			break;
		}

	}
}
